package com.cxyzy.tuchaodemo

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_main.*
import android.content.Intent
import android.net.Uri


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initTuGeCao()
    }

    private fun initTuGeCao() {
        webView.getSettings().setJavaScriptEnabled(true)
        webView.getSettings().setDomStorageEnabled(true)

        val webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
                super.shouldOverrideUrlLoading(view, url)

                if (url == null) {
                    return false
                }
                try {
                    if (url.startsWith("weixin://")) {
                        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                        view.context.startActivity(intent)
                        return true
                    }
                } catch (e: Exception) {
                    return false
                }

                view.loadUrl(url)
                return true
            }
        }
        webView.setWebViewClient(webViewClient)

        val url = "https://support.qq.com/products/42166"
        webView.loadUrl(url)
    }
}
